package com.sto.stoapp.activity.scanner;

import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.manager.AlarmClockManager;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoHhmEditText.EditClickListener;
import com.sto.stoapp.widget.StoHomeActivity;

/**
 * 自动上传时间设置
 * @author wxf
 *
 */
public class StoAutoUploadTimerSetActivity  extends StoHomeActivity{

	private StoHhmEditText setTimerEdit;
	private Button saveBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sto_auto_upload_timer_set);
		Bundle extras = getIntent().getExtras();
		this.setTitleBottom(R.string.auto_upload_time_list,
				extras.getString("busiType"));
		initWidget();
		initListener();
	}

	private void initWidget() {
		int time = -1;
		setTimerEdit = (StoHhmEditText) findViewById(R.id.set_upload_timer_edit);
		setTimerEdit.setInputType(EditorInfo.TYPE_CLASS_PHONE);
		time = StoFilePreference.getInstance(StoAutoUploadTimerSetActivity.this)
				.getAutoUploadTime();
		setTimerEdit.setHint(String.valueOf(time) + "(分)");
		saveBtn = (Button) findViewById(R.id.save);
	}

	private void initListener() {
		saveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				String setTimer = setTimerEdit.getText().toString().trim();
				if (isDigit(setTimer)) {
					int time = Integer.valueOf(setTimer).intValue();
					if (time < 5||time>60) {
						Helper.showSoundToast(
								StoAutoUploadTimerSetActivity.this,
								"时间必须大于5分钟小于60分钟!", false);

					} else {
						AlarmClockManager.getInstance(
								StoAutoUploadTimerSetActivity.this)
								.startAutoUploadAlarm(time);
						StoFilePreference.getInstance(
								StoAutoUploadTimerSetActivity.this)
								.setAutoUploadTime(time);
						Helper.showToast(StoAutoUploadTimerSetActivity.this,
								getString(R.string.set_success));
						finish();
					}

				}else{
					Helper.showSoundToast(StoAutoUploadTimerSetActivity.this, "时间必须是数字!",false);
				}
			}
		});
		
		setTimerEdit.setEditDelete(new EditClickListener() {
            
            @Override
            public void onEditDelete() {
                onBackPressed();
            }
            
            @Override
            public void onEditClickListener(View v) {
                
            }
        });
	}
	
    @Override
    public void onBackPressed() {
        showNotice();
    }
	
	@Override
	protected void onDestroy() {
		
		/*String setTimer = setTimerEdit.getText().toString();
		if (isDigit(setTimer)) {
			int time = Integer.valueOf(setTimer).intValue();
			if (time >= 1) {

				StoFilePreference.getInstance(
						StoAutoUploadTimerSetActivity.this)
						.setAutoUploadTime(time);
				Helper.showToast(
						StoAutoUploadTimerSetActivity.this,
						getString(R.string.set_success));
				Intent autpIntent = new Intent(
						UnifiedStaticType.TIMING_ACTION);
				autpIntent
						.putExtra(
								UnifiedStaticType.TIMING_TYPE,
								UnifiedStaticType.AUTO_UPLOAD_TIMING);
				
				sendBroadcast(autpIntent);

			}
		}*/
		super.onDestroy();
	}
}
